﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using snat.proxy;
using snat.proxy.items.exceptions;

namespace snat.proxy.items {
    public class Connection : Item, IComparable {

        private Entity from;
        public Entity From {
            get {
                return from;
            }
        }

        private Entity to;
        public Entity To {
            get {
                return to;
            }
        }

        private bool isInitial;
        public bool IsInitial {
            get {
                return isInitial;
            }
        }

        object strengthLock = new object();

        private double? strength = null;
        public double Strength {
            get {
                lock (strengthLock) {
                    if (strength == null) {
                        Instantiate();
                    }
                }
                return (double)strength;
            }
        }

        public Connection(DataRow _row, params DataSet[] _auxilliary){
                    this.strength = _row.Field<float>("Strength");
                    this.isInitial = _row.Field<bool>("Initial");
                    
                    int fromID, toID;
                    fromID = _row.Field<int>("FromEntityID");
                    toID = _row.Field<int> ("ToEntityID");
                    string fromName = null;
                    string toName = null;


                    if (_auxilliary.Length > 0) {
                        DataTable peopleTable = _auxilliary[0].Tables["Table"];
                        Console.WriteLine("The table returned had {0} rows", peopleTable.Rows.Count);
                        Console.WriteLine("Hopefully that was significant");


                        try {
                            var fromData = (from person in peopleTable.AsEnumerable()
                                            where person.Field<int>("EntityID") == fromID
                                            select person).Single();
                            fromName = fromData.Field<string>("EntityName");
                        }
                        catch (Exception e) {
                       //todo     Console.WriteLine("ERRIR, {0}", e.ToString());
                        }



                        try {
                            var toData = (from person in peopleTable.AsEnumerable()
                                          where person.Field<int>("EntityID") == toID
                                          select person).Single();
                                        toName = toData.Field<string>("EntityName");
                        }
                        catch (Exception) {
                            //todo 
                        }

 
                        

                    }

                    this.from = new Entity(fromID, fromName);
                    this.to = new Entity(toID, toName);
        }

        public Connection(Entity _from, Entity _to, bool _isInitial = false, double? _strength = null) {
            this.from = _from;
            this.to = _to;
            this.isInitial = _isInitial;
            this.strength = _strength;
        }

        public override String ToString() {
            return String.Join(" ", "From: ", From, "To: ", To, "Strength: ", strength, "Initial: ", IsInitial);
        }

        public int CompareTo(object obj) {
            return Strength.CompareTo((obj as Connection).Strength);
        }

        private void Instantiate() {
            try
            {
                DataSet strengthSet = DataProxy.Instance.RunQuery(
                    @"SELECT Strength FROM Connected
                                WHERE FromEntityID = " + From.ID + " AND ToEntityID = " + To.ID);
                strength = strengthSet.Tables["Table"].Rows[0].Field<float>("Strength");
            }
            catch (IndexOutOfRangeException)
            {
             
                throw new NoResultFromSQLException(@"SELECT * FROM Connected
                                WHERE FromEntityID = " + From.ID + " AND ToEntityID = " + To.ID);
            }
        }

        public override bool Equals(object obj) {
            return this.From.Equals((obj as Connection).From) && this.To.Equals((obj as Connection).To);
        }
    }
}
